/**
 * 客观告警
 */

;define(function(){
	
	//图标前缀
	var iconPrefix = CafeJS.CXT + '/static/widgets/cafejs/themes/default/images/objectivewarning/';
	
	/*
	 * 构建单个报警dom 
	 */
	function buildWarningItem(proObj){
		var $item = $('<div class="cafe-warning-ele-item"><div class="cafe-warning-ele-item-icon"></div><div class="cafe-warning-ele-item-title"></div></div>');
		if(proObj.icon.includes("http:")){
			$item.find(".cafe-warning-ele-item-icon").css("background-image", "url('" + proObj.icon + "')");
		}else{
			$item.find(".cafe-warning-ele-item-icon").css("background-image", "url('" + iconPrefix + proObj.icon + "')");
		}
		$item.attr("proId", proObj.proId);
		$item.find(".cafe-warning-ele-item-title").text(proObj.name);
		if($.isFunction(proObj.callback)){
			$item.off("click").on("click", function(){
				proObj.callback.call(this, proObj);
			});
		}
		return $item;
	};
	
	/**
	 * 获取模版
	 */
	function getTpl(){
		var tpl = '<div class="cafe-warning-container">'+
						'<div class="cafe-warning-ele-list"></div>'+
						'<div class="cafe-warning-ele-total-container">'+
							'<div class="cafe-warning-ele-icon"></div>'+
							'<div class="cafe-warning-ele-title">强天气预报</div>'+
							'<i class="cafe-warning-ele-total"></i>'+
						'</div>'+
					'</div>';
		return $(tpl);
	};
	
	/**
	 * 动画
	 */
	function animate($item, settings, callback){
		var right = 70 * (settings.pros.length - 1);
//		$item.css({top : -settings.fallHeight + "px"});
		$item.css({top : "0px"});
		$item.css({right : (settings.width + settings.moveStart) + "px"});
		setTimeout(function(){
//			$item.addClass("cafe-warning-icon-jump");
			$item.animate({right : right + "px"}, 1000, function(){
				if($.isFunction(callback)){
					callback.call(null);
				}
			});
		}, 500);
	}
	
	function init(opt){
		if($("#" + opt.containerId).length > 0){
			$("#" + opt.containerId).remove();
		}
		var $container = getTpl();
		$container.css({
			width : opt.width + "px",
			top : opt.top + "px",
			right : opt.right + "px"
		});
		$container.attr("id", opt.containerId);
		if(opt.renderTo instanceof $){
			opt.renderTo.append($container);
		}else{
			$(opt.renderTo).append($container);
		}
		if($.isFunction(opt.callback)){
			$container.find(".cafe-warning-ele-total-container").off("click").on("click", function(){
				opt.callback.call(this, opt.pros);
			});
		}
	}
	
	function checkProObj(pros, proObj){
		if(!$.isEmptyObject(proObj) && !$.isEmptyObject(proObj.icon) && 
				!$.isEmptyObject(proObj.name) && !$.isEmptyObject(proObj.proId)){
			var flag = true;
			pros.forEach(function(item){
				if(item.proId == proObj.proId){
					flag = false;
				}
			});
			return flag;
		}
		return false;
	}
	
	var ObjectiveWarning = function(opt){
		var defaults = {
			containerId : "",
			renderTo : "",
			width : 1000,
			top : 10,
			right : 10,
			pros : [],
			fallHeight : 100,
			moveStart : 300
		};
		this.settings = $.extend({}, defaults, opt);
		init(this.settings);
	}
	
	ObjectiveWarning.prototype = new Object();
	
	ObjectiveWarning.prototype.addOne = function(proObj){
		var that = this;
		if(typeof proObj == "object" && checkProObj(that.settings.pros, proObj)){
			that.settings.pros.push(proObj);
			var counts = that.settings.pros.length;
			var $item = buildWarningItem(proObj);
			$("#" + that.settings.containerId).find(".cafe-warning-ele-list").append($item);
			animate($item, that.settings, function(){
				$("#" + that.settings.containerId).find(".cafe-warning-ele-total").text(counts).show();
			});
		}
	}
	
	ObjectiveWarning.prototype.addList = function(proObjs){
		var that = this;
		if(Array.isArray(proObjs)){
			for(var i in proObjs){
				(function(index, data){
					setTimeout(function(){
						that.addOne(data[index]);
					}, 200 * i);
				})(i, proObjs);
			}
		}
	}
	
	ObjectiveWarning.prototype.clear = function(){
		var that = this;
		that.settings.pros = [];
		$("#" + that.settings.containerId).find(".cafe-warning-ele-total").text("").hide();
		$("#" + that.settings.containerId).find(".cafe-warning-ele-list").empty();
	}
	
	ObjectiveWarning.prototype.addWarn = function(data){
		var that = this;
		if(Array.isArray(data)){
			that.addList(data);
		}else{
			that.addOne(data);
		}
	}
	
	CafeJS.ObjectiveWarning = ObjectiveWarning;
});